<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:layout="http://www.ultraq.net.nz/web/thymeleaf/layout"
      layout:decorate="~{layout/default}">

<head>
    <meta charset="UTF-8">
    <title>帝一流</title>
    <link th:href="@{/assets/libs/nestable/nestable.css}" type="text/css" rel="stylesheet">
</head>

<body>
<div layout:fragment="breadcrumb" class="page-breadcrumb">
    <div class="row">
        <div class="col-5 align-self-center">
            <h4 class="page-title">网址排序</h4>
        </div>
        <div class="col-7 align-self-center">
            <div class="d-flex align-items-center justify-content-end">
                <nav aria-label="breadcrumb">
                    <ol class="breadcrumb">
                        <li class="breadcrumb-item">
                            <a href="#">Home</a>
                        </li>
                        <li class="breadcrumb-item">
                            <a href="#">Guide</a>
                        </li>
                        <li class="breadcrumb-item active" aria-current="page">sort</li>
                    </ol>
                </nav>
            </div>
        </div>
    </div>
</div>
<div layout:fragment="container" class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="border-bottom">
                    <div class="card-body text-right">
                        <button type="button" class="btn btn-primary" id="saveSort">保存</button>
                    </div>
                </div>

                <div class="card-body">
                    <div class="nestable">
                        <div class="dd" id="nestable">
                            <ol class="dd-list">
                                <li class="dd-item" th:each="type:${#request.getAttribute('types')}" th:attr="data-id=${type.id}">
                                    <div class="dd-handle" th:text="${type.name}"></div>
                                    <ol class="dd-list" th:if="${!#lists.isEmpty(type.siteList)}">
                                        <li class="dd-item" th:each="site:${type.siteList}" th:attr="data-id=${site.id}">
                                            <div class="dd-handle" th:text="${site.name}"></div>
                                        </li>
                                    </ol>
                                </li>
                            </ol>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<div layout:fragment="allJs">
    <script th:src="@{/assets/libs/nestable/jquery.nestable.js}"></script>
    <script th:inline="javascript">
        $(function () {
            var str = '';
            var updateOutput = function(e)
            {
                var list   = e.length ? e : $(e.target);
                str = JSON.stringify(list.nestable('serialize'));
            };

            $('#nestable').nestable({
                group: 1
            }).on('change', updateOutput);
            updateOutput($('#nestable'));

            $('#saveSort').on('click', function () {
                // 提示
                swal({
                        title: "确认当前排列?",
                        text: "保存后将无法恢复",
                        type: "warning",
                        showCancelButton: true,
                        confirmButtonColor: "#e67e22",
                        confirmButtonText: "确定",
                        closeOnConfirm: true
                    },
                    function () {
                        $.ajax({
                            type: 'post',
                            url: /*[[@{/guide/type}]]*/,
                            contentType: "application/json;charset=utf-8",
                            data: str,
                            dataType: "json",
                            success: function (result) {
                                if (result == 1) {
                                    alertTip('success', '保存成功');
                                } else {
                                    alertTip('error', '保存失败');
                                }
                            }
                        });
                    });
            });
        });
    </script>
</div>
</body>
</html>